#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : xuexiaohua
# @time    : 2025/8/20 14:09
# @function: the script is used to do something.
# @version :
import requests
import xmltodict
from requests import Response

def response_to_dict(response: Response):
    '''
    获取 Response对象，将多种响应格式统一转换为dict
    :param response:
    :return:
    '''
    res_text = response.text
    if res_text.startswith('<?xml'):
        final_res = xmltodict.parse(res_text)
    else:
        final_res = response.json()
    return final_res

# 测试响应是否可以转换为dict
def response_dict():
    """
    xml转换为json
    :return:
    """
    # res = requests.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
    res = requests.get("https://httpbin.ceshiren.com/get")
    r = response_to_dict(res)
    print(r)
    assert isinstance(r, dict)

if __name__ == '__main__':
    response_dict()

